﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Admin;
using OpenSoftStore.BusinessLogic.Money;
using OpenSoftStore.BusinessLogic.Provider;
using OpenSoftStore.Client.UIL.ManageAdmin;
using OpenSoftStore.Client.UIL.UserControls;
using OpenSoftStore.Orm.Model;
using OpenSoftStore.Orm.Static;
using OpenSoftStore.Orm.View;
using OpenSoftStore.Client.Common;

namespace OpenSoftStore.Client.UIL.ManageProvider
{
    public partial class PayIn : UserControl
    {
        private string ProviderID;
        private List<T_Admin> adminList;
        private List<T_Account> moenyList;
        private T_Provider providerModel;
        private ProviderLogic providerLogic;
        public List<V_PayIn> PayIns;
        private V_Money moneyModel;
        private MoneyLogic moenyLogic;
        public Boolean Drog = false;
        public int Top = 0;
        public int Left = 0;

        public PayIn()
        {
            InitializeComponent();
            providerLogic = new ProviderLogic();
            PayIns = new List<V_PayIn>();
            moenyList = new List<T_Account>();
            adminList = new List<T_Admin>();
            moneyModel = new V_Money();
            moenyLogic = new MoneyLogic();
            SPACommon.SetDataGridStyle(dgvProviderPayIn);
        }

        public void GetProviderId(string id)
        {
            ProviderID = id;
            GetPrviderPayIn();
            GetData();
        }
        #region 默认加载数据
        private void GetPrviderPayIn()
        {
            dgvProviderPayIn.AutoGenerateColumns = false;
            button1_Click(null, null);
        }

        private void GetData()
        {
            var adminLogic = new AdminLogic();
            adminList = adminLogic.GetAdminList().ToList();
            cbAdminList.DisplayMember = "Name";
            cbAdminList.ValueMember = "Id";
            cbAdminList.DataSource = adminList;
            cbAdminList.Text = S_Admin.Name;

            moenyList = moenyLogic.SqlWhere<T_Account>(" 1=1").ToList();
            var list = (from v in moenyList where v.Name != "现金" select v.Name).ToList();
            cbBank.DataSource = list;

            this.txtNo.Text = moenyLogic.GetCountNo("T_ProviderPay", true);
            providerModel = providerLogic.SqlWhere<T_Provider>("  Id='" + ProviderID + "'").ToList()[0];
            this.txtProvider.Text = providerModel.Name;
            this.lbKy.Text = providerModel.YuFu.ToString();
        }
        private void HfPayInfo()
        {
            //lbYf.Text = "0";
            //tbCashPay.Text = "0";
            //tbBankPay.Text = "0";
            //tbje.Text = "0";
            //lbSy.Text = "0";
        }
        #endregion

        private void dgvProviderPayIn_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvProviderPayIn.EndEdit(DataGridViewDataErrorContexts.Commit);
            var select = PayIns.Where(m => m.Select == "是");
            lbYf.Text = select.Sum(m => m.WfPay).ToString();
        }

        private void radioButton1_Click(object sender, EventArgs e)
        {
            this.dgvProviderPayIn.ReadOnly = true;
            GetPrviderPayIn();
            this.label12.Text = "现金付款";
            this.label21.Text = "付款结余";
            numFavorMoney.Enabled = false;
            GetData();
            HfPayInfo();
        }


        private void radioButton3_Click(object sender, EventArgs e)
        {
            this.dgvProviderPayIn.ReadOnly = true;
            this.label12.Text = "现金退款";
            this.label21.Text = "退款结余";
            numFavorMoney.Enabled = false;
            GetPrviderPayIn();
            GetData();
            HfPayInfo();
        }
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void glassButton1_Click(object sender, EventArgs e)
        {
            #region 供应商支付赋值
            var providerPay = new T_ProviderPay();
            try
            {
                providerPay.NeedPay = decimal.Parse(this.lbYf.Text);
                providerPay.RealPay = numCashPay.Value + numBankPay.Value;
                providerPay.FavorMoney = numFavorMoney.Value;
                providerPay.BeforLastValue = decimal.Parse(lbKy.Text);
                providerPay.AfterLastValue = decimal.Parse(lbSy.Text);
                providerPay.CashPay = numCashPay.Value;
                providerPay.BankPay = numBankPay.Value;
                providerPay.AddTime = dtpPay.Value;
                providerPay.Remark = this.txtRemark.Text;

            }
            catch (Exception)
            {
                MessageBox.Show("请输入合法数字类型");
                return;
            }
            providerPay.Id = Guid.NewGuid().ToString("D");
            if (providerPay.RealPay + providerPay.FavorMoney < providerPay.NeedPay)
            {
                MessageBox.Show("支付金额不足，可做预付款处理！");
                return;
            }
            if (rbPrePayReturn.Checked)
                providerPay.BuzType = rbPrePayReturn.Text;
            else if (rbPay.Checked)
                providerPay.BuzType = rbPay.Text;
            else if (rbPrePay.Checked)
                providerPay.BuzType = rbPrePay.Text;
            providerPay.PayNo = this.txtNo.Text.Trim();
            providerPay.ProviderId = this.ProviderID;
            string paytype = "";
            if (numCashPay.Value > 0 && numBankPay.Value == 0)
                paytype = "现金";
            else if (numBankPay.Value > 0 && numCashPay.Value == 0)
                paytype += "银行转账";
            else if (numBankPay.Value > 0 && numCashPay.Value > 0)
                paytype += "现金+银行转账";
            providerPay.PayType = paytype;

            var adminId = cbAdminList.SelectedValue.ToString();
            providerPay.AdminId = adminId;
            moneyModel.AdminId = adminId;
            providerPay.ShopId = S_Admin.ShopId;
            moneyModel.ShopId = S_Admin.ShopId;

            foreach (var v in moenyList)
            {
                if (v.Name == cbBank.Text)
                {

                    providerPay.BankAccountId = v.Id;
                    moneyModel.AccountId = v.Id;
                }
            }
            #endregion

            #region 仓库收支赋值
            dgvProviderPayIn.EndEdit(DataGridViewDataErrorContexts.Commit);
            var select = PayIns.Where(m => m.Select == "是").ToList();
            #endregion

            #region 收支账单赋值

            moneyModel.PayType = paytype;
            moneyModel.ObjectId = providerPay.Id;
            #endregion

            if (rbPay.Checked) //付款
            {
                moneyModel.ObjectId2 = "付款给供应商";
                moneyModel.Type = "支出";
                providerPay.Type = "支出";
                providerModel.YuFu = (numCashPay.Value + numBankPay.Value + numFavorMoney.Value + decimal.Parse(this.lbKy.Text)) - decimal.Parse(lbYf.Text);
                providerLogic.Save(select, providerPay, providerModel);
            }
            else if (rbPrePay.Checked)//预付款
            {
                moneyModel.ObjectId2 = "付预付款";
                moneyModel.Type = "支出";
                providerPay.Type = "支出";
                providerModel.YuFu += numBankPay.Value + numCashPay.Value;
                providerLogic.Save(select, providerPay, providerModel);
            }
            else if (rbPrePayReturn.Checked)//预付款退款
            {
                moneyModel.ObjectId2 = "收回预付款";
                moneyModel.Type = "收入";
                providerPay.Type = "收入";
                if (numCashPay.Value + numBankPay.Value >
                    decimal.Parse(this.lbKy.Text))
                {
                    MessageBox.Show("退款金额不能大于结余款！");
                    return;
                }
                else
                {
                    providerModel.YuFu = decimal.Parse(this.lbKy.Text) - (numCashPay.Value + numBankPay.Value);
                    providerLogic.Save(select, providerPay, providerModel);
                }
            }
            var findForm = this.FindForm();
            if (findForm != null) findForm.Close();
            ProviderList.Instance.button4_Click_1(null, null);
        }

        private void pbClose_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void PnlInfoTitle_MouseDown(object sender, MouseEventArgs e)
        {
            Drog = true;
            Top = e.Y;
            Left = e.X;
        }

        private void PnlInfoTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (!Drog) return;
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.Top += e.Y - Top;
            findForm.Left += e.X - Left;
        }

        private void PnlInfoTitle_MouseUp(object sender, MouseEventArgs e)
        {
            Drog = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var from = dtpFrom.Value.Date;
            var to = dtpTo.Value.Date.AddDays(1).AddSeconds(-1);

            dgvProviderPayIn.DataSource = providerLogic.GetProviderPayIn(ProviderID, " and Type ='入库' and AddTime between #" + from + "# and #" + to + "#");
        }

        private void numCashPay_ValueChanged(object sender, EventArgs e)
        {
            var payMoney = decimal.Parse(lbKy.Text) + numCashPay.Value + numBankPay.Value + numFavorMoney.Value;
            lbSy.Text = (payMoney - decimal.Parse(lbYf.Text)).ToString();
        }

        private void numCashPay_KeyUp(object sender, KeyEventArgs e)
        {
            numCashPay_ValueChanged(null, null);
        }

        private void rbPay_Click(object sender, EventArgs e)
        {
            this.dgvProviderPayIn.ReadOnly = false;
            GetPrviderPayIn();
            GetData();
            HfPayInfo();
            this.label12.Text = "现金付款";
            this.label21.Text = "付款结余";
            numFavorMoney.Enabled = true;
        }

    }
}
